/**
 * Created by ASUS on 2017/12/28.
 */
function initVueObj(obj){
    var defaultObj = {
        vueTarget1:null,
        vueTarget2:null
    };
    for(var i in obj){
        defaultObj[i] = obj[i]
    }


    var vueObj2 = new Vue({
        el:defaultObj.vueTarget2,
        data:{
            addressList:[
                // {
                //     name:"小红",
                //     phone:"18519793652",
                //     isDefault:true,
                //     isSelect:true,
                //     address:"云南省昆明市盘龙区城区颐欣里白云路与北京路交叉口16栋3单元502"
                // },
                // {
                //     name:"小明",
                //     phone:"18519793652",
                //     isDefault:false,
                //     isSelect:false,
                //     address:"云南省昆明市盘龙区城区颐欣里白云路与北京路交叉口16栋3单元502"
                // },
                // {
                //     name:"小光",
                //     phone:"18519793652",
                //     isDefault:false,
                //     isSelect:false,
                //     address:"云南省昆明市盘龙区城区颐欣里白云路与北京路交叉口16栋3单元502"
                // }
            ]
        },
        methods:{
            setDefault:function(address, url){
                $.post(url, {addressId: address.id}, function (data){
                    layer.open({
                        skin:"msg",
                        content:data.msg,
                        time: 1.5
                    })
                }).error(function() {
                    layer.open({
                        skin:"msg",
                        content:"服务器链接失败",
                        time: 1.5
                    })
                })


                // var _this = this;
                // this.addressList.map(function(ele,index){
                //     if(index == _this.addressList.indexOf(address)){
                //         _this.addressList[index].def = 1
                //     }else{
                //         _this.addressList[index].def = 0
                //     }
                // });
                resetSlideSize();
            },
            removeAddress:function(address, url){

                $.post(url, {addressId: address.id}, function (data){
                    layer.open({
                        skin:"msg",
                        content:data.msg,
                        time: 1.5
                    })
                }).error(function() {
                    layer.open({
                        skin:"msg",
                        content:"服务器链接失败",
                        time: 1.5
                    })
                })

                // var _this = this;
                // _this.addressList.map(function(ele,index){
                //     if(index == _this.addressList.indexOf(address)){
                //         _this.addressList.splice(index,1);
                //         if(_this.addressList.length==1){
                //             _this.addressList[0].def = 1;
                //             _this.addressList[0].isSelect = true;
                //         }
                //         return false;
                //     }
                // });
                resetSlideSize();
            },
            selectAddress:function(address){
                var _this = this;
                this.addressList.map(function(ele,index){
                    if(index == _this.addressList.indexOf(address)){
                        _this.addressList[index].isSelect = true
                    }else{
                        _this.addressList[index].isSelect = false
                    }
                });
            }

        }
    });

    $.get(addressListUrl,function(msg){
        // var obj = JSON.parse(msg);
        msg.obj.map(function(ele,index){
            if(msg.obj[index]["def"]==1){
                msg.obj[index]["isSelect"] = true;
            }else{
                msg.obj[index]["isSelect"] = false;
            }
        });

        vueObj2.addressList = msg.obj;

        setTimeout(function(){
            addressChooseInit({
                target:".swiper-container"
            });
        },100);

    });

    var vueObj1 = new Vue({
        el:defaultObj.vueTarget1,
        data:{
            totalOrderCount:3,
            totalPrice:1000,
            orderFreight:0,
            orderMaster:"小明",
            orderMasterPhone:"18514684497",
            addressList:vueObj2.addressList,
            orders:[
                {
                    hospitalID:"1",
                    hospitalName:"仁爱医院",
                    hospitalProductTotalPrice:function(products){
                        var num = 0;
                        products.map(function(ele,index){
                            num = num + ele.salesPrice
                        });
                        return num;
                    },
                    hospitalProductDisCountNoCard:function(products){
                        var num = 0;
                        products.map(function(ele,index){
                            num = num + ele.disCountPrice(ele.oldPrice,ele.salesPrice)
                        });
                        return num;
                    },
                    hospitalProductHasPhysicalExaminationCard:function(products){
                        var flag = false;
                        products.map(function(ele,index){
                            if(Number(ele.productType)){
                                flag = true;
                                return false;
                            }
                        });
                        return flag;
                    },
                    disCountCards:[
                        {
                            condition:500,
                            disCountNum:100
                        },
                        {
                            condition:300,
                            disCountNum:50
                        }
                    ],
                    products:[
                        {
                            productID:"11",
                            productType:1,
                            isAppointNow:false,
                            salesPrice:400,
                            oldPrice:500,
                            count:2,
                            productName:"套餐",
                            productImg:"http://www.qydjk.org/resource/front/sho/image/product/20170620/2017062013052288021717.png",
                            disCountPrice:function(oldPrice,salesPrice){
                                return oldPrice-salesPrice
                            },
                            appointInfo:{
                                appointName:"小明",
                                appointSex:"1",
                                appointPhone:"18549796582",
                                appointAge:"24",
                                appointIdentificationCard:"530381199403294533",
                                appointTime:"2017-10-10"
                            }
                        },
                        {
                            productID:"11",
                            productType:0,
                            isAppointNow:false,
                            salesPrice:400,
                            oldPrice:500,
                            count:2,
                            productName:"爱健康体检卡",
                            productImg:"http://www.qydjk.org/resource/front/sho/image/product/20170620/2017062013052288021717.png",
                            disCountPrice:function(oldPrice,salesPrice){
                                return oldPrice-salesPrice
                            },
                            appointInfo:{
                                appointName:"小明",
                                appointSex:"1",
                                appointPhone:"18549796582",
                                appointAge:"24",
                                appointIdentificationCard:"530381199403294533",
                                appointTime:"2017-10-10"
                            }
                        }

                    ]
                },
                {
                    hospitalID:"2",
                    hospitalName:"星耀医院",
                    hospitalProductTotalPrice:function(products){
                        var num = 0;
                        products.map(function(ele,index){
                            num = num + ele.salesPrice
                        });
                        return num;
                    },
                    hospitalProductDisCountNoCard:function(products){
                        var num = 0;
                        products.map(function(ele,index){
                            num = num + ele.disCountPrice(ele.oldPrice,ele.salesPrice)
                        });
                        return num;
                    },
                    hospitalProductHasPhysicalExaminationCard:function(products){
                        var flag = false;
                        products.map(function(ele,index){
                            if(Number(ele.productType)){
                                flag = true;
                                return false;
                            }
                        });
                        return flag;
                    },
                    disCountCards:[
                        {
                            condition:500,
                            disCountNum:100
                        },
                        {
                            condition:300,
                            disCountNum:50
                        }
                    ],
                    products:[
                        {
                            productID:"11",
                            productType:1,
                            isAppointNow:false,
                            salesPrice:400,
                            oldPrice:500,
                            count:2,
                            productName:"豪华体检套餐",
                            productImg:"http://www.qydjk.org/resource/front/sho/image/product/20170620/2017062013052288021717.png",
                            disCountPrice:function(oldPrice,salesPrice){
                                return oldPrice-salesPrice
                            },
                            appointInfo:{
                                appointName:"小明",
                                appointSex:"1",
                                appointPhone:"18549796582",
                                appointAge:"24",
                                appointIdentificationCard:"530381199403294533",
                                appointTime:"2017-10-10"
                            }
                        },
                        {
                            productID:"12",
                            productType:1,
                            isAppointNow:false,
                            salesPrice:400,
                            oldPrice:500,
                            count:2,
                            productName:"套餐",
                            productImg:"http://www.qydjk.org/resource/front/sho/image/product/20170620/2017062013052288021717.png",
                            disCountPrice:function(oldPrice,salesPrice){
                                return oldPrice-salesPrice
                            },
                            appointInfo:{
                                appointName:"小明",
                                appointSex:"1",
                                appointPhone:"18549796582",
                                appointAge:"24",
                                appointIdentificationCard:"530381199403294533",
                                appointTime:"2017-10-10"
                            }
                        }
                    ]
                }
            ]
        },
        methods:{
            makeAppoint:function(product){
                product.isAppointNow = !product.isAppointNow;
            }
        }

    });
    $(".switchBoxClickArea").on("tap",function(e){
        var e = e||window.event;
        var target = e.target||e.srcElement;
        var tar;
        if(($(target).hasClass("switchBoxClickArea")||$(target).parents(".switchBoxClickArea").length)&&!($(target).hasClass("mui-switch")||$(target).parents(".mui-switch").length)){
            if($(target).parents(".switchBoxClickArea").length){
                tar = $(target).parents(".switchBoxClickArea");
            }else{
                tar = $(target);
            }
            mui(tar.find(".mySwitchBtn")[0]).switch().toggle();
        };
        if(tar == undefined){
            tar = $(target).parents(".switchBoxClickArea");
        }
        setTimeout(function(){
            if(tar.find(".mySwitchBtn").hasClass("mui-active")&&!tar.next().hasClass("appointShowBox")){
                mui(tar.click());
            }else if(!tar.find(".mySwitchBtn").hasClass("mui-active")&&tar.next().hasClass("appointShowBox")){
                mui(tar.click());
            }
        },100);
    });
}